/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package basedatos;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import logica.cModelo;

/**
 *
 * @author Alberto
 */
public class GestorModelos {

    private static GestorModelos INSTANCE = null;
    private String user;
    private String password;
    private String url;

    private GestorModelos() {
        this.user = "root";
        this.password = "root";
        this.url = "jdbc:mysql://localhost:3306/repuestos_bd";
    }

    public static GestorModelos getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new GestorModelos();
        }
        return INSTANCE;
    }

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            System.out.println("Error en la conexion");
        }
    }

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    public void insertarModelo(cModelo c) {
        String query = "INSERT INTO modelos (cod_modelo, nombre_modelo)"
                + "VALUES ('" + c.getCodModelo() + "', '" + c.getNombreModelo() + "')";
//        System.out.println("insertarModelo() query=" + query);      
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            pStmt.execute(query);

        } catch (Exception e) {
            /* CAPTURAR EXCEPTION */
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public void borrarModelo(cModelo c) throws SQLException {
        String query = "DELETE FROM modelos WHERE cod_modelo= '" + c.getCodModelo() + "'";
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            pStmt.execute(query);

        } catch (Exception e) {
            /* CAPTURAR EXCEPTION */
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public void actulaizarModelo(cModelo c) throws SQLException {
        String query = "UPDATE modelos SET nombre_modelo= '" + c.getNombreModelo()
                + "'where cod_modelo= '" + c.getCodModelo() + "'";
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            pStmt.executeUpdate(query);

        } catch (Exception e) {
            /* CAPTURAR EXCEPTION */
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public Collection<cModelo> getmodelo() {
        Collection<cModelo> modelos = new ArrayList<cModelo>();
//        System.out.println("Modelos "+modelos.size());
        String query = "SELECT cod_modelo, nombre_modelo from modelos order by cod_modelo";
//        System.out.println(" QUERY: "+query);
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            ResultSet set = pStmt.executeQuery(query);
            while (set.next()) {
                cModelo c = new cModelo();
                c.setCodModelo(set.getInt("cod_modelo"));
                c.setNombreModelo(set.getString("nombre_modelo"));
                modelos.add(c);
            }
        } catch (Exception e) {
            System.out.println("ERROR: " + e.getMessage());
            /* CAPTURAR EXCEPTION */
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
            return modelos;
        }
    }

    public boolean existeModelo(int codModelo) {
        String query = "SELECT * from modelos where cod_modelo = '" + codModelo + "'";
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            ResultSet set = pStmt.executeQuery(query);
            if (set.next()) {
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
        }
    }
}